<?php

namespace App\Http\Controllers\Wxapi;

use App\Models\Admin\Configs;
use App\Models\Admin\Picture;
use Illuminate\Http\Request;

class FilesController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    /* 附件上传 */
    public function uploadAnnex(Request $request)
    {
        $con = Configs::query()->where("mall_id", $request->mallData["id"])->first();
        $env = $con->env;
        if ($env == 'CLOUD'){  //配置云开发版
            $base64_img = trim($request->post('file',''));
            if (!$base64_img){
                $this->result['status'] = 1;
                $this->result['msg'] = '缺少文件';
                return $this->return_result($this->result);
            }
            $files = json_decode($base64_img,true);
            $temp_file = tempnam(sys_get_temp_dir(),"php");  //临时文件
            $content = $files['content'];
            file_put_contents($temp_file,base64_decode($content));        //文件流写入文件
            //创建文件夹
            $time = date('Ymd',time());
            $cloud_file = '/uploads/wxapi/annex/'.$time;
            $img_name = $cloud_file.'/'.time().$files['name'];
            $pictureModel = new Picture();
            $url = $pictureModel->uploadImg($img_name,$temp_file);
            if (!$url){
                $this->result['status'] = 1;
                $this->result['msg'] = '上传文件失败';
                return $this->return_result($this->result);
            }
            $result['status'] = 0;
            $result['msg'] = '请求成功';
            $result['data']['url'] = $url['ObjectURL'];
            $result['data']['id'] = $url['ObjectURL'];
        } else{
            $file = $request->file('file');
            if (!$file){
                $this->result['status'] = 1;
                $this->result['msg'] = '缺少文件';
                return $this->return_result($this->result);
            }
            $dir_name = date('Ymd',time());
            $inputFileName = $request->file('file');
            //文件扩展名
            $ext = $inputFileName->getClientOriginalExtension();
            //获取文件的绝对路径
            $path = $inputFileName->getRealPath();
            //定义文件名
            $filename = time().'.'.$ext;
            $new_path = public_path().'/uploads/wxapi/annex/'.$dir_name;
            if (!is_dir($new_path)){
                mkdir($new_path,0777,true);
            }
            //移动保存文件
            if (move_uploaded_file($path,$new_path.'/'.$filename) && file_exists($new_path.'/'.$filename)){
                $path = 'uploads/wxapi/annex/'.$dir_name.'/'.$filename;
                $result['status'] = 0;
                $result['msg'] = '上传成功';
                $result['data'] = ['url' => $this->processingPictures($path),'id'=>$path];
            }else{
                $result['status'] = 1;
                $result['msg'] = '上传失败';
            }
        }
        return $this->return_result($result);
    }

    /* 音频上传 */
    public function uploadAudio(Request $request){
        $con = Configs::query()->where("mall_id", $request->mallData["id"])->first();
        $env = $con->env;
        if ($env == 'CLOUD'){  //配置云开发版
            $base64_img = trim($request->post('file',''));
            if (!$base64_img){
                $this->result['status'] = 1;
                $this->result['msg'] = '缺少文件';
                return $this->return_result($this->result);
            }
            $files = json_decode($base64_img,true);
            $temp_file = tempnam(sys_get_temp_dir(),"php");  //临时文件
            $content = $files['content'];
            file_put_contents($temp_file,base64_decode($content));        //文件流写入文件
            //创建文件夹
            $time = date('Ymd',time());
            $cloud_file = '/uploads/wxapi/audio/'.$time;
            $img_name = $cloud_file.'/'.time().$files['name'];
            $pictureModel = new Picture();
            $url = $pictureModel->uploadImg($img_name,$temp_file);
            if (!$url){
                $this->result['status'] = 1;
                $this->result['msg'] = '上传文件失败';
                return $this->return_result($this->result);
            }
            $result['status'] = 0;
            $result['msg'] = '请求成功';
            $result['data']['url'] = $url['ObjectURL'];
            $result['data']['id'] = $url['ObjectURL'];
        } else{
            $file = $request->file('file');
            if (!$file){
                $this->result['status'] = 1;
                $this->result['msg'] = '缺少文件';
                return $this->return_result($this->result);
            }
            $dir_name = date('Ymd',time());
            $inputFileName = $request->file('file');
            //文件扩展名
            $ext = $inputFileName->getClientOriginalExtension();
            //获取文件的绝对路径
            $path = $inputFileName->getRealPath();
            //定义文件名
            $filename = time().'.'.$ext;
            $new_path = public_path().'/uploads/wxapi/audio/'.$dir_name;
            if (!is_dir($new_path)){
                mkdir($new_path,0777,true);
            }
            //移动保存文件
            if (move_uploaded_file($path,$new_path.'/'.$filename) && file_exists($new_path.'/'.$filename)){
                $path = 'uploads/wxapi/audio/'.$dir_name.'/'.$filename;
                $result['status'] = 0;
                $result['msg'] = '上传成功';
                $result['data'] = ['url' => $this->processingPictures($path),'id'=>$path];
            }else{
                $result['status'] = 1;
                $result['msg'] = '上传失败';
            }
        }
        return $this->return_result($result);
    }
}
